package com.anpy.bill.service.impl;

import com.anpy.bill.entity.DateLineTypeEntity;
import com.anpy.bill.mapper.DateLineTypeMapper;
import com.anpy.bill.service.DateLineTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 日历事件类型服务实现类
 */
@Service
public class DateLineTypeServiceImpl extends ServiceImpl<DateLineTypeMapper, DateLineTypeEntity> implements DateLineTypeService {

    /**
     * 根据用户ID获取所有日历事件类型
     */
    @Override
    public List<DateLineTypeEntity> getDateLineTypesByUserId(Integer userId) {
        QueryWrapper<DateLineTypeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderByAsc("type");
        return this.list(queryWrapper);
    }

    /**
     * 根据ID获取日历事件类型详情
     */
    @Override
    public DateLineTypeEntity getDateLineTypeDetail(Long id, Integer userId) {
        QueryWrapper<DateLineTypeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.eq("user_id", userId);
        return this.getOne(queryWrapper);
    }

    /**
     * 创建日历事件类型
     */
    @Override
    public boolean createDateLineType(DateLineTypeEntity typeEntity) {
        return this.save(typeEntity);
    }

    /**
     * 更新日历事件类型
     */
    @Override
    public boolean updateDateLineType(DateLineTypeEntity typeEntity, Integer userId) {
        // 验证类型是否属于当前用户
        DateLineTypeEntity existing = this.getDateLineTypeDetail(typeEntity.getId(), userId);
        if (existing == null) {
            return false;
        }
        
        // 更新类型信息
        typeEntity.setUserId(userId);
        return this.updateById(typeEntity);
    }

    /**
     * 删除日历事件类型
     */
    @Override
    public boolean deleteeDateLineType(Long id, Integer userId) {
        // 验证类型是否属于当前用户
        DateLineTypeEntity existing = this.getDateLineTypeDetail(id, userId);
        if (existing == null) {
            return false;
        }
        
        // 删除类型
        return this.removeById(id);
    }
}
